AUTO_CLOSE_DELAY_SECONDS = 10;//自动隐藏时间 秒
g_last_id = null;
MySpaceURL = null;
g_last_id =0;
/*(╯▔皿▔)╯ 有一大波丧失正在靠近 ㄟ(▔皿▔ㄟ)*/
var oldNotice;
var oscpng = new Image();
oscpng.src = "images/osc.png";
if(localStorage.getItem("uid")){
  oscapi.uid = localStorage.getItem("uid");
};
function oscapi(url,data){
  var xml;
  $.ajax({
    type:"POST",
    url: url,
    cache:false,
    data: data,
    async:false,
    success: function(e){xml = e;},
    dataType: "xml"
  });
  return xml;
}
oscapi.portraitimg = new Image();
oscapi.xml = [];
oscapi.portraitimg.onload = function () {
  var txcanvas=document.getElementById("txcanvas");
  var context2d = txcanvas.getContext("2d");
  context2d.fillStyle = "#ffffff";
  context2d.fillRect(0, 0, 19, 19);
  context2d.drawImage(oscapi.portraitimg,0,0,29,29);
  context2d.drawImage(oscpng,6,20);
  chrome.browserAction.setIcon({imageData:context2d.getImageData(6,6,19,19)});
}

// oscapi.login = function(username,pwd){
//   var xml = this(LOGIN_VALIDATE_HTTP,{username:username,pwd:pwd,keep_login:1});
//   var $xml = $(xml);
//   var errorCode = $xml.find("result errorCode").text();
//   var errorMessage = $xml.find("result errorMessage").text();
//   if(errorCode ==1){
//     $user = $xml.find("user");
//     this.uid = $user.find("uid").text();
//     localStorage.setItem("uid", this.uid);
//     this.name = $user.find("name").text();//昵称
//     this.portrait = $user.find("portrait").text();//头像
//     this.portraitimg.src =oscapi.portrait;
//     $notice = $xml.find("notice");
//     this.notice = {
//       atmeCount:$notice.find("atmeCount").text(),
//       msgCount:$notice.find("msgCount").text(),
//       reviewCount:$notice.find("reviewCount").text(),
//       newFansCount:$notice.find("newFansCount").text()
//     }
//     return true;
//   }else{
//       console.log(errorMessage);
//       return false;
//   }
// }

oscapi.has = function(e){
  return !(typeof this[e] === 'undefined');
}

oscapi.getUserNotice =function(){
  if(this.has('uid')){
    var xml = this(USER_NOTICE,{uid:this.uid});
    var $xml = $(xml);
      $notice = $xml.find("notice");
      this.notice = {
        atmeCount:$notice.find("atmeCount").text(),
        msgCount:$notice.find("msgCount").text(),
        reviewCount:$notice.find("reviewCount").text(),
        newFansCount:$notice.find("newFansCount").text()
      }
      return this.notice;
  }
  return false;
}

oscapi.getActiveList = function(catalog,pageIndex,pageSize){
  if(typeof catalog === 'undefined'){catalog = 1;}
  if(typeof pageIndex === 'undefined'){pageIndex = 0;}
  if(typeof pageSize === 'undefined'){pageSize = 10;}
  if(this.has('uid')){
    var xml = this(ACTIVE_LIST,{uid:this.uid,catalog:catalog,pageIndex:pageIndex,pageSize:pageSize});
    return xml;
  }
  return false;
}

oscapi.pubTweet = function(tweet){
  if (!tweet) {return false};
  if(this.has('uid')){
    var xml = this(TWEET_PUB,{uid:this.uid,msg:tweet});
    var $xml = $(xml);
    var errorCode = $xml.find("result errorCode").text();
    var errorMessage = $xml.find("result errorMessage").text();
    if(errorCode == 1){
      return true;
    }
  }
  return false;
}

oscapi.getTweetList = function(uid,pageIndex,pageSize){
  if(typeof pageIndex === 'undefined'){pageIndex = 0;}
  if(typeof pageSize === 'undefined'){pageSize = 10;}
  if(typeof uid === 'undefined'){
    var xml = this(TWEET_LIST,{pageIndex:pageIndex,pageSize:pageSize});
  }else{
    var xml = this(TWEET_LIST,{uid:uid,pageIndex:pageIndex,pageSize:pageSize});
  }
  return xml;
}

oscapi.getMessageList = function(pageIndex,pageSize){
  if(typeof pageIndex === 'undefined'){pageIndex = 0;}
  if(typeof pageSize === 'undefined'){pageSize = 10;}
  if(this.has('uid')){
    var xml = this(MESSAGE_LIST,{uid:this.uid,pageIndex:pageIndex,pageSize:pageSize});
    return xml;
  }
  return false;
}



oscapi.ListNotify = function(xml){
  $list = $(xml).find("activies active");
    for (var i=0;i<$list.length;i++)
    {
      var objectID = $($list[i]).find("objectID").text();
      var authorid = $($list[i]).find("authorid").text();
      notify({
        "url":"http://my.oschina.net/u/"+authorid+"/tweet/"+objectID,
        "icon":($($list[i]).find("portrait").text()).replace("_50","_100"),
        "title":"OSC - "+$($list[i]).find("author").text(),
        "body":$($list[i]).find("message").text()
      })
    }
  // console.log($list);
}
oscapi.myInformation = function(){
  if(this.has('uid')){
    var xml = this(MY_INFORMATION,{uid:this.uid});
    return xml;
  }
  return false;
}

function check_new_log(){
  var Notice = oscapi.getUserNotice();
  oscapi.notice = Notice;
  var Sum = parseInt(Notice.atmeCount)+parseInt(Notice.msgCount)+parseInt(Notice.reviewCount)+parseInt(Notice.newFansCount);
  if(!Sum){Sum = ""}
  chrome.browserAction.setBadgeText({text:Sum+""});
  if (true) {};
  var gap = {};
  gap.atmeCount    = Notice.atmeCount    - oldNotice.atmeCount;
  gap.msgCount     = Notice.msgCount     - oldNotice.msgCount;
  gap.reviewCount  = Notice.reviewCount  - oldNotice.reviewCount;
  gap.newFansCount = Notice.newFansCount - oldNotice.newFansCount;
  if(gap.atmeCount    <=0){gap.atmeCount     = 0}else{oscapi.ListNotify(oscapi.getActiveList(2,0,gap.atmeCount))};
  // if(gap.msgCount     <=0){gap.msgCount      = 0}else{oscapi.ListNotify(oscapi.getActiveList(3,0,gap.msgCount))};
  if(gap.reviewCount  <=0){gap.reviewCount   = 0}else{oscapi.ListNotify(oscapi.getActiveList(3,0,gap.reviewCount))};
  // if(gap.newFansCount <=0){gap.newFansCount  = 0}else{oscapi.ListNotify(oscapi.getActiveList())};
  oldNotice =Notice;
}
function getlogin(){
  chrome.cookies.get({
    url:URL_API_HOST,
    name:"oscid"
  }, function(e){
    if (e) {
      $.get(URL_API_HOST,function(data){
        localStorage.setItem("uid",$(data).find("[name='user']").val());
        oscapi.uid = localStorage.getItem("uid");
        var oschina = xml2arr(oscapi.myInformation())["oschina"];
        var user = oschina.user;
        oscapi.notice = oschina.notice;
        oscapi.portraitimg.src =user.portrait;
        start();
      });
    };
  });
}
function start(){
  oldNotice=oscapi.getUserNotice();
  check_new_log();
  setInterval(check_new_log,10000);
}
getlogin();